package com.his.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.his.api.service.PatientService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Patient;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import lombok.AllArgsConstructor;
import org.hibernate.sql.Update;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/patient")
@AllArgsConstructor
public class PatientController {
    private PatientService patientService;

    @RequestMapping("/login")
    public RestResult login(@RequestBody Patient patient) {
        return patientService.login(patient);
    }

    @RequiresLogin
    @RequestMapping("/info")
    public RestResult info(@RequestHeader("authorization") String authorization) {
        return patientService.info(authorization);
    }

    @RequiresLogin
    @RequestMapping("/register")
    public RestResult register(@RequestBody Patient patient) {
        if (patientService.save(patient)) {
            return new RestResultBuilder().success();
        }

        return new RestResultBuilder().fail();
    }

    @RequestMapping("/change")
    public RestResult changePassword(@RequestBody Patient patient) {
        UpdateWrapper<Patient> updateWrapper = new UpdateWrapper<Patient>()
                .eq("username", patient.getUsername())
                .set("password", patient.getPassword());
        if (patientService.update(updateWrapper)) {
            return new RestResultBuilder().success();
        }

        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequestMapping("/update")
    public RestResult update(@RequestBody Patient patient) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<Patient>()
                .eq("ID", patient.getId());
        if (patientService.update(patient, queryWrapper)) {
            return new RestResultBuilder().success();
        }

        return new RestResultBuilder().fail();
    }
}